package vincent.com.layouttest.view;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

import vincent.com.layouttest.R;
import vincent.com.layouttest.util.LogUtil;

/**
 * Created by zhengwenchen on 2016/5/9.
 */
public class UVView extends View {

    private Paint mPaint;

    private float[] mLines;

    private int mVerticalOffset;

    private int mLineStrokeWidth;

    private final int linesPerLevel = 5;

    private final int maxLevel = 10;
    private final int minLevel = 0;

    private int totalCount;

    private int coloredLines;

    //只能是整数值？
    private int mLevel = 0;

    private ObjectAnimator mAnimTor;

    private int computedHeight;

    public int getmLevel() {
        return mLevel;
    }

    public int getColoredLines() {
        return coloredLines;
    }

    public void setColoredLines(int coloredLines) {
        LogUtil.i("czw_coloredlines", "setMColoredLines value: " + coloredLines);
        this.coloredLines = coloredLines;
        ViewCompat.postInvalidateOnAnimation(this);
    }

    public void scrollTo(int level) {

        this.mLevel = maskLevel(level);
        coloredLines = this.mLevel * linesPerLevel;

        ViewCompat.postInvalidateOnAnimation(this);
    }

    public UVView(Context context) {
        this(context, null);
    }

    public UVView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public UVView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        mLineStrokeWidth = getContext().getResources().getDimensionPixelSize(R.dimen.uv_level_stroke_width);
        mPaint.setStrokeWidth(mLineStrokeWidth);

        mVerticalOffset = (int)(4 * getResources().getDisplayMetrics().density);

        totalCount = 4 * maxLevel * linesPerLevel;
        mAnimTor = ObjectAnimator.ofInt(this, "coloredLines", 0, 0);
        mAnimTor.setDuration(300);
        mAnimTor.setInterpolator(new AccelerateDecelerateInterpolator());

        computedHeight = maxLevel * linesPerLevel * (mVerticalOffset + mLineStrokeWidth) - mVerticalOffset;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mLines = new float[totalCount];

        float y = 0f;
        int index = 0;
        for(int i = 0; i < mLines.length; i+=4){
             index = i/4;

            y = mLineStrokeWidth/2 + (mVerticalOffset + mLineStrokeWidth) * index;

            mLines[i] = 0f;
            mLines[i+1] = y;
            mLines[i+2] = (float)w;
            mLines[i+3] = y;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        LogUtil.i("czw_uvview", "onMeasure width: " + widthSpecSize + " | height: " + computedHeight);

        setMeasuredDimension(widthSpecSize, computedHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        int count = totalCount - coloredLines * 4;
        mPaint.setColor(getResources().getColor(R.color.uv_gray_color));
        canvas.drawLines(mLines, 0, count, mPaint);

        mPaint.setColor(getResources().getColor(R.color.uv_color));
        canvas.drawLines(mLines, count, coloredLines * 4, mPaint);
    }

    public void smoothScrollTo(int level){

        this.mLevel = maskLevel(level);
        int lines = this.mLevel * linesPerLevel;

        mAnimTor.setIntValues(coloredLines, lines);
        mAnimTor.start();
    }

    private int maskLevel(int level){
        if(level < minLevel)
            level = minLevel;

        if(level > maxLevel)
            level = maxLevel;

        return level;
    }

    public String test(){
        String string = getContext().getResources().getString(R.string.my_test_string);
        return string;
    }
}